/*
 * Condition.h
 *
 *  Created on: 2021年4月5日
 *      Author: san
 */

#ifndef BASE_CONDITION_H_
#define BASE_CONDITION_H_

#include "Mutex.h"
#include <pthread.h>

namespace san {

class Condition {
public:
	explicit Condition(MutexLock &mutex)
		: mutex_(mutex)
	{
		pthread_cond_init(&pcond_, NULL);
	}

	~Condition()
	{
		pthread_cond_destroy(&pcond_);
	}

	void wait()
	{
		pthread_cond_wait(&pcond_, mutex_.get());
	}

	void notify()
	{
		pthread_cond_signal(&pcond_);
	}

	void notifyAll()
	{
		pthread_cond_broadcast(&pcond_);
	}

	bool waitForSeconds(int seconds);

private:
	MutexLock &mutex_;
	pthread_cond_t pcond_;
};

} /* namespace san */

#endif /* BASE_CONDITION_H_ */
